package com.pine.dbant.plugin.mysql.function;

import com.pine.dbant.common.jdbc.JdbcUtils;
import com.pine.dbant.common.plugin.meta.JdbcMetadataAnalysisConfig;
import com.pine.dbant.common.plugin.meta.QueryMetadataParam;
import com.pine.dbant.common.plugin.meta.function.jdbc.CharsetFunction;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xiaoyuan
 * @create 2023/3/20 9:34
 **/
public class MysqlCharsetFunction extends CharsetFunction {



    @Override
    public List query(JdbcMetadataAnalysisConfig metadataAnalysisConfig, QueryMetadataParam queryMetadataParam) {
      Map<String,List<Map>>  temp  =  JdbcUtils.getSqlRunner(metadataAnalysisConfig.getConnection()).queryList("show charset;").stream()
                .collect(Collectors.groupingBy(m-> String.valueOf(m.get("Charset")), LinkedHashMap::new, Collectors.toList()));
        return temp.keySet().stream().map(key->{
            Map<String,Object> obj = new HashMap<>();
            obj.put("name",key);
            obj.put("children",temp.get(key));
            return obj;
        }).collect(Collectors.toList());
    }
}
